class NadeCureW extends NadeW;

var () int HealBoostAmount;

simulated function Explode(vector HitLocation, vector HitNormal)
{
	if(bHasExploded)
		return;
	bHasExploded=True;
	//Log("Exloded");
	if (ROLE == ROLE_Authority)
	{
		//Spawn(Class'NadeCureWProj',,, HitLocation + HitNormal*20, rotator(HitNormal));
		Spawn(Class'HealFieldProj',,, HitLocation + HitNormal*20, rotator(HitNormal));
	}
	PlaySound(ExplodeSounds[rand(ExplodeSounds.length)],,100.5*TransientSoundVolume);
	if ( EffectIsRelevant(Location,false) )
	{
		Spawn(Class'PerksSimHostOrg.HealingExplosionEmitter',,, HitLocation + HitNormal*20, rotator(HitNormal));
	}
	BlowUp(HitLocation);
	Destroy();
}

simulated function HurtRadius( float DamageAmount, float DamageRadius, class<DamageType> DamageType, float Momentum, vector HitLocation )
{
	local actor Victims;
	local float damageScale, dist;
	local vector dirs;
	local int NumKilled;
	local KFMonster KFMonsterVictim;
	local Pawn P;
	local KFPawn KFP;
	local array<Pawn> CheckedPawns;
	local int i;
	local bool bAlreadyChecked;
	local KFPlayerReplicationInfo PRI;
	local int MedicReward;
	local KFHumanPawn Healed;
	local float HealSum; // for modifying based on perks

	if ( bHurtEntry )
		return;
	bHurtEntry = true;

	if(KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo).ClientVeteranSkill!=Class'SRVetFieldMedic')
		return;
	foreach CollidingActors (class 'Actor', Victims, DamageRadius, HitLocation)
	{
		// don't let blast damage affect fluid - VisibleCollisingActors doesn't really work for them - jag
		if( (Hurtwall != Victims) && (Victims.Role == ROLE_Authority) && 
			!Victims.IsA('FluidSurfaceInfo') && ExtendedZCollision(Victims)==None )
		{
			dirs = Victims.Location - HitLocation;
			dist = FMax(1,VSize(dirs));
			dirs = dirs/dist;
			damageScale = 1 - FMax(0,(dist - Victims.CollisionRadius)/DamageRadius);
			if ( Instigator == None || Instigator.Controller == None )
				Victims.SetDelayedDamageInstigatorController( InstigatorController );
			if ( Victims == LastTouched )
				LastTouched = None;

			P = Pawn(Victims);

			if( P != none )
			{
				for (i = 0; i < CheckedPawns.Length; i++)
				{
					if (CheckedPawns[i] == P)
					{
						bAlreadyChecked = true;
						break;
					}
				}

				if( bAlreadyChecked )
				{
					bAlreadyChecked = false;
					P = none;
					continue;
				}

				KFMonsterVictim = KFMonster(Victims);

				if( KFMonsterVictim != none && KFMonsterVictim.Health <= 0 )
				{
					KFMonsterVictim = none;
				}

				KFP = KFPawn(Victims);

				if( KFMonsterVictim != none )
				{
					damageScale *= KFMonsterVictim.GetExposureTo(HitLocation/*Location + 15 * -Normal(PhysicsVolume.Gravity)*/);
				}
				else if( KFP != none )
				{
					damageScale *= KFP.GetExposureTo(HitLocation/*Location + 15 * -Normal(PhysicsVolume.Gravity)*/);
				}

				CheckedPawns[CheckedPawns.Length] = P;

				if ( damageScale <= 0)
				{
					P = none;
					continue;
				}
				else
				{
					//Victims = P;
					P = none;
				}
			}
			if (KFHumanPawn(Victims) != none)
			{
				Healed = KFHumanPawn(Victims);
				if (Instigator != none && Healed != none && Healed.Health > 0 &&
					Healed.Health <  Healed.HealthMax)
				{
					MedicReward = HealBoostAmount;
					PRI = KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo);
					if ( PRI != none && PRI.ClientVeteranSkill != none )
					{
						MedicReward *= PRI.ClientVeteranSkill.Static.GetHealPotency(PRI);
					}

					MedicReward *= damageScale; /* Reduce healing strength for then distance from explotion center increases */
					HealSum = MedicReward;

					if ((Healed.Health + Healed.healthToGive + MedicReward) > Healed.HealthMax )
					{
						MedicReward = Healed.HealthMax - (Healed.Health + Healed.healthToGive);
						if ( MedicReward < 0 )
						{
							MedicReward = 0;
						}
					}

					//Healed.GiveHealth(HealSum, Healed.HealthMax);
					if (Pawn(Victims).Health < KFHumanPawn(Victims).HealthMax)
						Pawn(Victims).Health = Min(KFHumanPawn(Victims).HealthMax, Pawn(Victims).Health + HealSum * 0.5);

/*                  if (Healed.ShieldStrength >= 0)
					{
					if (Healed.ShieldStrength < 75)
						Healed.AddShieldStrength(ShieldBoostAmount * damageScale * PRI.ClientVeteranSkill.Static.GetHealPotency(PRI) );
					}
*/
					if ( PRI != None )
					{
						if (MedicReward > 0 && 
							KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements) != none &&
							Victims != Instigator && Victims.Base != Instigator )
						{
							KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements).AddDamageHealed(MedicReward/2, true);
							ServerStStats(KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements)).AddDamageHealedWithM56(MedicReward/2);
						}

						// Give the medic reward money as a percentage of how much of the person's health they healed
						MedicReward = int((FMin(float(MedicReward),Healed.HealthMax)/Healed.HealthMax) * 60); // Increased to 80 in Balance Round 6, reduced to 60 in Round 7
						PRI.Score += MedicReward;
						PRI.ThreeSecondScore += MedicReward;
						PRI.Team.Score += MedicReward;

						if ( KFHumanPawn(Instigator) != none )
						{
							KFHumanPawn(Instigator).AlphaAmount = 255;
						}

						if( MP7MW(Instigator.Weapon) != none )
						{
							MP7MW(Instigator.Weapon).ClientSuccessfulHeal(Healed.PlayerReplicationInfo.PlayerName);
						}
					}
				//Spawn(Class'PerksSimHostOrg.EXHealingFX',,, Victims.Location + dirs*20, rotator(dirs));
				}
			}
			else
			{
				Victims.TakeDamage(damageScale * DamageAmount,Instigator,
								Victims.Location - 0.5 * (Victims.CollisionHeight + Victims.CollisionRadius) * dirs,
								(damageScale * Momentum * dirs),DamageType);
				if (Vehicle(Victims) != None && Vehicle(Victims).Health > 0)
					Vehicle(Victims).DriverRadiusDamage(DamageAmount, DamageRadius, InstigatorController, DamageType, Momentum, HitLocation);

				if( Role == ROLE_Authority && KFMonsterVictim != none && KFMonsterVictim.Health <= 0 )
				{
					NumKilled++;
				}
			}
		}
	}
	if ( (LastTouched != None) && (LastTouched != self) && (LastTouched.Role == ROLE_Authority) && !LastTouched.IsA('FluidSurfaceInfo') )
	{
		Victims = LastTouched;
		LastTouched = None;
		dirs = Victims.Location - HitLocation;
		dist = FMax(1,VSize(dirs));
		dirs = dirs/dist;
		damageScale = FMax(Victims.CollisionRadius/(Victims.CollisionRadius + Victims.CollisionHeight),1 - FMax(0,(dist - Victims.CollisionRadius)/DamageRadius));
		if (KFHumanPawn(Victims) != none)
		{
			Healed = KFHumanPawn(Victims);
			if (Instigator != none && Healed != none && Healed.Health > 0 && Healed.Health <  Healed.HealthMax)
			{
				MedicReward = HealBoostAmount;
				PRI = KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo);
				if ( PRI != none && PRI.ClientVeteranSkill != none )
				{
					MedicReward *= PRI.ClientVeteranSkill.Static.GetHealPotency(PRI);
				}
				MedicReward *= damageScale; /* Reduce healing strength for then distance from explotion center increases */
				HealSum = MedicReward;
				if ( (Healed.Health + Healed.healthToGive + MedicReward) > Healed.HealthMax )
				{
					MedicReward = Healed.HealthMax - (Healed.Health + Healed.healthToGive);
					if ( MedicReward < 0 )
					{
						MedicReward = 0;
					}
				}
				//Healed.GiveHealth(HealSum, Healed.HealthMax);
				if (Pawn(Victims).Health < KFHumanPawn(Victims).HealthMax)
					Pawn(Victims).Health = Min(KFHumanPawn(Victims).HealthMax, Pawn(Victims).Health + HealSum * 0.5);
/*           if (Healed.ShieldStrength >= 0)
			{
			if (Healed.ShieldStrength < 75)
				Healed.AddShieldStrength(ShieldBoostAmount * damageScale * PRI.ClientVeteranSkill.Static.GetHealPotency(PRI) );
			}
*/
				if ( PRI != None )
				{
					if (MedicReward > 0 && 
						KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements) != none &&
						Victims != Instigator && Victims.Base != Instigator )
					{
						KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements).AddDamageHealed(MedicReward/2, true);
						ServerStStats(KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements)).AddDamageHealedWithM56(MedicReward/2);
					}
					// Give the medic reward money as a percentage of how much of the person's health they healed
					MedicReward = int((FMin(float(MedicReward),Healed.HealthMax)/Healed.HealthMax) * 60); // Increased to 80 in Balance Round 6, reduced to 60 in Round 7
					PRI.Score += MedicReward;
					PRI.ThreeSecondScore += MedicReward;
					PRI.Team.Score += MedicReward;
					if ( KFHumanPawn(Instigator) != none )
					{
						KFHumanPawn(Instigator).AlphaAmount = 255;
					}
					if( MP7MW(Instigator.Weapon) != none )
					{
						MP7MW(Instigator.Weapon).ClientSuccessfulHeal(Healed.PlayerReplicationInfo.PlayerName);
					}
				}
			//Spawn(Class'PerksSimHostOrg.EXHealingFX',,, Victims.Location + dirs*20, rotator(dirs));
			}
		}
		else
		{
			if ( Instigator == None || Instigator.Controller == None )
				Victims.SetDelayedDamageInstigatorController(InstigatorController);
			Victims.TakeDamage(damageScale * DamageAmount,Instigator,
							Victims.Location - 0.5 * (Victims.CollisionHeight + Victims.CollisionRadius) * dirs,
							(damageScale * Momentum * dirs),DamageType);
			if (Vehicle(Victims) != None && Vehicle(Victims).Health > 0)
				Vehicle(Victims).DriverRadiusDamage(DamageAmount, DamageRadius, InstigatorController, DamageType, Momentum, HitLocation);
		}
	}

	if( Role == ROLE_Authority )
	{
		if( NumKilled >= 4 )
		{
			KFGameType(Level.Game).DramaticEvent(0.05);
		}
		else if( NumKilled >= 2 )
		{
			KFGameType(Level.Game).DramaticEvent(0.03);
		}
	}
	bHurtEntry = false;
}

defaultproperties
{
     HealBoostAmount=20
     ExplodeTimer=1.200000
     Damage=650.000000
     MyDamageType=Class'PerksSimHostOrg.DamTypeAcidGunW'
}
